package zhr.chapter9;

import javax.swing.*;
//BoxLayout盒式布局
public class BoxLayoutFrame extends JFrame {
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    public BoxLayoutFrame(){
        button1 = new JButton("Button1");
        button2 = new JButton("Button2");
        button3 = new JButton("Button3");
        button4 = new JButton("Button4");
        button5 = new JButton("Button5");
        //创建第一个水平盒式容器
        Box hBox01=Box.createHorizontalBox();
        hBox01.add(button1);
        hBox01.add(button2);
        hBox01.add(button3);
        //创建第二个水平盒式容器
        Box hBox02=Box.createHorizontalBox();
        hBox02.add(button4);
        //添加一共水平方向胶状的不可见组件，撑满剩余水平空间
        hBox02.add(Box.createHorizontalGlue());
        hBox02.add(button5);
        //创建一个垂直盒式容器，放置上面两个水平盒式容器(Box组合嵌套)
        Box vBox=Box.createVerticalBox();
        vBox.add(hBox01);
        vBox.add(hBox02);

        //把垂直盒式容器作为内容面板设置到窗体
        this.setContentPane(vBox);
        this.setTitle("BoxLayout盒式布局");
        this.setSize(600,400);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }
}
